  
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
	  


var centralizouTabNoMapa = false;
var mapPontosEntregas = null;
//var arrMarkersDepot = new Array();
var numeraMarkers = {depot : 0, job : 0};
var arrMarkers = new Array();
var resultsGeoCode;

function isNumber(n) {
	  return !isNaN(parseFloat(n)) && isFinite(n);
}


function insereMarkerNoArray(qMarker, qArray) {
	for(var i = 0; i < qArray.length; i++) 
		if(qArray[i].__gm_id==qMarker.__gm_id) {						
			return false;
		}
	
	qArray.push(qMarker);
	return true;
}

function removeMarkerDoArray(qMarker, qArray) {
	for(var i = 0; i < qArray.length; i++) 
		if(qArray[i].__gm_id==qMarker.__gm_id) {
			qArray.remove(i);
			return true;
		}
	
	return false;

}

function retornaMarkerDoArray(qGmId, qArray) {
	for(var i = 0; i < qArray.length; i++) 
		if(qArray[i].__gm_id==qGmId) {			
			return qArray[i];
		}
	
	return null;
}

function centralizaMarker(qGmId, qIxi) {
	if(!resultsGeoCode) return;
	var marker = retornaMarkerDoArray(qGmId, arrMarkers);
	if(!marker) {
		$("#map_canvas").gmap("findMarker", "__gm_id", qGmId, false, function(a,b) {if(b) marker=a;});
	}
	if(marker) {
		marker.setPosition(resultsGeoCode[qIxi].geometry.location);
		mapPontosEntregas.setCenter(resultsGeoCode[qIxi].geometry.location);
		
		$.each(resultsGeoCode[qIxi].address_components, function(i,v) {

			if ( v.types[0] == "administrative_area_level_1" || v.types[0] == "administrative_area_level_2" ) {

				$('#state'+qGmId).val(v.long_name);
				estado = v.long_name;

			} /*else if ( v.types[0] == "country") {

				$('#country'+marker.__gm_id).val(v.long_name);

			}*/

		});
		
		marker.setTitle(resultsGeoCode[qIxi].formatted_address);

		$('#address'+marker.__gm_id).val(resultsGeoCode[qIxi].formatted_address);
		$('#resultsEndereco'+marker.__gm_id).hide();
	}
	
	

}
